﻿using ConAppCore.Json;
using System;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.Ex.CommandLineParser;
using UtilZ.Dotnet.Ex.Model;

namespace ConAppCore.Test
{
    internal class TestCommandParser
    {
        public static void Test(string[] args)
        {
            CommandParser.Default.RegistCommand<TestCommand>();
            CommandParser.Default.RegistCommand<TestCommand2>();
            CommandParser.Default.RegistCommand<TestCommand3>();

            string splitLine = "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
            Console.WriteLine(splitLine);
            Console.WriteLine(CommandParser.Default.GetAllCommandHelpText());
            Console.WriteLine(splitLine);
            Console.WriteLine();

            object obj;
            string err;
            bool parseRet;
            if (args == null || args.Length == 0)
            {
                string commandLine = "-t -p \"D:\\temp\\新建    文件夹\" -c 1 -s F";
                commandLine = "-t -c 1 -s F";
                commandLine = "-t -p \"D:\\temp\\新建    \\\"文件夹\\\"abc\" -c 1 -s M";
                parseRet = CommandParser.Default.TryParse(commandLine, out obj, out err);
            }
            else
            {
                parseRet = CommandParser.Default.TryParse(args, out obj, out err);
            }

            if (parseRet)
            {
                var command = (TestCommand)obj;
                Console.WriteLine(command.ToString());
            }
            else
            {
                Console.WriteLine($"{err}");
            }


        }
    }

    [Command("-t", "-test", "测试命令")]
    public class TestCommand : ICommandLineOptionConverter
    {
        [CommandLineOption("-p", "-path", "路径", @"win:D:\temp；linux:/home/yf/temp", Order = 1), Required]
        public string Path { get; set; } = null;

        [CommandLineOption("-c", "-count", "次数", @"测试次数，取值范围为1-3", Order = 2), ValueRange(1, 3)]
        public int Count { get; set; } = 1;

        [CommandLineOption("-s", "-sex", "性别", @"性别[男:M;女:F]", ConverterType = typeof(TestCommand), Order = 3), Required, EnumRange(Sexs.Man)]
        public Sexs Sex { get; set; } = Sexs.Man;


        public TestCommand()
        {

        }

        /// <summary>
        /// 尝试从字符串转换为值类型数据,包括枚举
        /// </summary>
        /// <param name="proInfo">属性信息/param>
        /// <param name="value">要转换的值</param>
        /// <param name="result">转换结果</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        bool ICommandLineOptionConverter.TryConvertTo(PropertyInfo proInfo, string value, out object result)
        {
            bool ret = true;
            if (string.Equals("M", value, StringComparison.OrdinalIgnoreCase))
            {
                result = Sexs.Man;
            }
            else if (string.Equals("F", value, StringComparison.OrdinalIgnoreCase))
            {
                result = Sexs.Woman;
            }
            else
            {
                ret = false;
                result = null;
            }

            return ret;
        }

        public override string ToString()
        {
            return ObjectEx.ToString(this);
        }

    }

    [Command("-t2", "-test2", "测试命令2")]
    public class TestCommand2 
    {
        [CommandLineOption("-p", "-path", "路径", @"win:D:\temp；linux:/home/yf/temp", Order = 1), Required]
        public string Path { get; set; } = null;

        [CommandLineOption("-c", "-count", "次数", @"测试次数，取值范围为1-3", Order = 2), ValueRange(1, 3)]
        public int Count { get; set; } = 1;


        public TestCommand2()
        {

        }

        public override string ToString()
        {
            return ObjectEx.ToString(this);
        }

    }

    [Command("-t3", "-test3", "测试命令3")]
    public class TestCommand3
    {
        [CommandLineOption("-p", "-path", "路径", @"win:D:\temp；linux:/home/yf/temp", Order = 1), Required]
        public string Path { get; set; } = null;

        [CommandLineOption("-c", "-count", "次数", @"测试次数，取值范围为1-3", Order = 2), ValueRange(1, 3)]
        public int Count { get; set; } = 1;


        public TestCommand3()
        {

        }

        public override string ToString()
        {
            return ObjectEx.ToString(this);
        }

    }
}
